package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.OldmanAbilityBaseinfoEntity;
import city.spring.modules.oldman.service.OldmanAbilityBaseinfoService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：能力评估基础信息
 *
 * @author pxm
 * @date 2020-06-29 10:20:07
 */
@RestController
@RequestMapping("/oldman_ability_baseinfo")
public class OldmanAbilityBaseinfoController extends ApiController {
    private final OldmanAbilityBaseinfoService oldmanAbilityBaseinfoService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanAbilityBaseinfoEntity, ?>> entityOrderFields;

    public OldmanAbilityBaseinfoController(OldmanAbilityBaseinfoService oldmanAbilityBaseinfoService) {
        this.oldmanAbilityBaseinfoService = oldmanAbilityBaseinfoService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getId);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getCode);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getReason);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getBasedate);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getType);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getOldman);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getInfoProvider);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getRelation);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getProviderName);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getPhone);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getGmtCreate);
        entityOrderFields.add(OldmanAbilityBaseinfoEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>能力评估基础信息</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, OldmanAbilityBaseinfoEntity entity) {
        LambdaQueryChainWrapper<OldmanAbilityBaseinfoEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>能力评估基础信息</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, OldmanAbilityBaseinfoEntity entity) {
        LambdaQueryChainWrapper<OldmanAbilityBaseinfoEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanAbilityBaseinfoEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>能力评估基础信息</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanAbilityBaseinfoService.getById(id));
    }

    /**
     * 添加一个 <strong>能力评估基础信息</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanAbilityBaseinfoEntity entity) {
        oldmanAbilityBaseinfoService.saveOldmanAbilityBaseinfo(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>能力评估基础信息</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanAbilityBaseinfoEntity entity) {
        entity.setId(id);
        oldmanAbilityBaseinfoService.updateOldmanAbilityBaseinfo(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>能力评估基础信息</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanAbilityBaseinfoService.deleteOldmanAbilityBaseinfo(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>能力评估基础信息</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanAbilityBaseinfoService.deleteOldmanAbilityBaseinfo(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanAbilityBaseinfoEntity> buildLambdaQuery(OldmanAbilityBaseinfoEntity entity) {
        LambdaQueryChainWrapper<OldmanAbilityBaseinfoEntity> lambdaQuery = oldmanAbilityBaseinfoService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanAbilityBaseinfoEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getCode()), OldmanAbilityBaseinfoEntity::getCode, entity.getCode());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReason()), OldmanAbilityBaseinfoEntity::getReason, entity.getReason());
        lambdaQuery.eq(entity.getBasedate() != null, OldmanAbilityBaseinfoEntity::getBasedate, entity.getBasedate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), OldmanAbilityBaseinfoEntity::getType, entity.getType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldman()), OldmanAbilityBaseinfoEntity::getOldman, entity.getOldman());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getInfoProvider()), OldmanAbilityBaseinfoEntity::getInfoProvider, entity.getInfoProvider());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRelation()), OldmanAbilityBaseinfoEntity::getRelation, entity.getRelation());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getProviderName()), OldmanAbilityBaseinfoEntity::getProviderName, entity.getProviderName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPhone()), OldmanAbilityBaseinfoEntity::getPhone, entity.getPhone());
        lambdaQuery.eq(entity.getGmtCreate() != null, OldmanAbilityBaseinfoEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, OldmanAbilityBaseinfoEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanAbilityBaseinfoEntity> buildQuery(OldmanAbilityBaseinfoEntity entity) {
        QueryWrapper<OldmanAbilityBaseinfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getCode()), "code", entity.getCode());
        wrapper.like(StringUtils.isNotBlank(entity.getReason()), "reason", entity.getReason());
        wrapper.eq(entity.getBasedate() != null, "basedate", entity.getBasedate());
        wrapper.like(StringUtils.isNotBlank(entity.getType()), "type", entity.getType());
        wrapper.like(StringUtils.isNotBlank(entity.getOldman()), "oldman", entity.getOldman());
        wrapper.like(StringUtils.isNotBlank(entity.getInfoProvider()), "Info_provider", entity.getInfoProvider());
        wrapper.like(StringUtils.isNotBlank(entity.getRelation()), "relation", entity.getRelation());
        wrapper.like(StringUtils.isNotBlank(entity.getProviderName()), "provider_name", entity.getProviderName());
        wrapper.like(StringUtils.isNotBlank(entity.getPhone()), "phone", entity.getPhone());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
